package com.admin.action;

import com.admin.bean.FindInfor;
import com.admin.service.FindAdminService;
import com.client.action.FindLookAction;
import com.data.util.JsonUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 发现管理
 *
 * @author: HingLo
 * @create 2017-10-19 9:17
 **/
@Controller
@RequestMapping("/findAdmin/admin/")
public class FindAdminAction {
    private static Logger logger = Logger.getLogger(FindAdminAction.class);
    @Autowired
    private FindAdminService findAdminService;

    @Autowired
    private FindLookAction findLookAction;

    /**
     * 进入到发现列表
     *
     * @param request
     * @return
     */
    @GetMapping("inputFind")
    public String inputFindlistAction(HttpServletRequest request) {
        List<FindInfor> list = this.findAdminService.inputFindlistService();
        request.setAttribute("dataMap", list);
        return "/WEB-INF/admin/role/findlist";
    }

    /**
     * 查看资讯的banner图
     *
     * @param id
     * @param response
     */

    @GetMapping("lookFindIndexImage")
    public void lookFindIndexImageAction(String id, HttpServletResponse response) {
        findLookAction.lookContentImageAction(id, response);
    }

    /**
     * 查看发现的具体内容
     *
     * @param id
     * @param request
     * @return
     */
    @RequestMapping("lookfindContents")
    public String lookfindContentsAction(String id, HttpServletRequest request) {
        return findLookAction.lookfindContentsAction(id, request);
    }


    /**
     * 进入到添加资讯界面
     *
     * @param request
     * @return
     */
    @GetMapping("inputAddFind")
    public String inputAddFindlistAction(HttpServletRequest request) {
        return "/WEB-INF/admin/role/findadd";
    }


    /**
     * 删除资讯
     *
     * @param id
     * @return
     */
    @PostMapping("deleteFind")
    @ResponseBody
    public Map<String, Object> deleteFindAction(String id) {
        boolean bool = this.findAdminService.deleteFindService(id);
        return JsonUtils.returnMassageUtils(bool, "删除");
    }

    /**
     * 通过审核的资讯
     *
     * @param id
     * @return
     */
    @PostMapping("startFind")
    @ResponseBody
    public Map<String, Object> startFindAction(String id, boolean state) {
        boolean bool = findAdminService.startFindService(id, state);
        return JsonUtils.returnMassageUtils(bool, "通过");
    }

    /**
     * 上传发现内容中的图片，视频，等文件
     *
     * @param filedata
     * @return
     */
    @PostMapping("imageUpload")
    @ResponseBody
    public Map<String, Object> imageUploadAction(@RequestParam("filedata") CommonsMultipartFile filedata, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        map.put("err", "");
        map.put("msg", "");
        if (filedata != null) {
            String url = findAdminService.imageUploadService(filedata);
            if (url != null) {
                url = JsonUtils.getBasePath(request) + "/findLook/client/lookImage?id=" + url;
                map.put("msg", url);
                return map;
            }
        }
        map.put("err", "图片上传失败");
        return map;
    }


    /**
     * 访问资讯中的图片
     *
     * @param id
     */
    @GetMapping("lookImage")
    public void lookImageAction(String id, HttpServletResponse response) {
        findLookAction.lookImageAction(id, response);


    }

    /**
     * 提交发现
     *
     * @param file
     * @param findInfor
     * @param request
     * @return
     */

    @PostMapping("submitFindContent")
    public String submitFindContentAction(@RequestParam("file") CommonsMultipartFile file, FindInfor findInfor, HttpServletRequest request) {
        boolean bool = findAdminService.submitFindContentService(findInfor, file);
        request.setAttribute("bool", bool);
        return "/WEB-INF/admin/role/findadd";
    }

}
